/*
 * $Id: ArgumentTestCase.java 4 2007-12-20 21:20:26Z aaron.day $
 */
package org.crazydays.text;


import java.util.List;
import java.util.ArrayList;

import junit.framework.TestCase;

/**
 * ArgumentTestCase
 */
public class ArgumentTestCase
	extends TestCase
{
	/**
	 * Test constructor.
	 */
	public void testConstructor()
	{
		Argument argument = null;

		argument = new Argument("test", 't', false);
		assertEquals("test != argument.string", "test", argument.getString());
		assertEquals("t != argument.character", (Character) 't', (Character) argument.getCharacter());
		assertEquals("false != argument.required", false, argument.getRequired());

		argument = new Argument("test", 't', true);
		assertEquals("test != argument.string", "test", argument.getString());
		assertEquals("t != argument.character", (Character) 't', (Character) argument.getCharacter());
		assertEquals("true != argument.required", true, argument.getRequired());
	}

	/**
	 * Test validate.
	 */
	public void testValidate()
	{
		Argument argument = null;
		List<String> options = null;

		// not required
		argument = new Argument("test", 't', false);

		try {
			argument.validate();
		} catch (ArgumentException e) {
			fail("ArgumentException: " + e.getMessage());
		}

		// required: fail
		argument = new Argument("test", 't', true);

		try {
			argument.validate();
			fail("Expected ArgumentException");
		} catch (ArgumentException e) {
			assertEquals("e.arugment != argument", argument, e.getArgument());
		}

		// required
		argument = new Argument("test", 't', true);
		argument.setValue("test");

		try {
			argument.validate();
		} catch (ArgumentException e) {
			fail("ArgumentException: " + e.getMessage());
		}

		// options
		options = new ArrayList<String>();
		options.add("abc");
		options.add("xyz");
		argument = new Argument("test", 't', false, options);

		// unset value
		try {
			argument.validate();
		} catch (ArgumentException e) {
			fail("ArgumentException: " + e.getMessage());
		}

		// set invalid value
		argument.setValue("invalid");
		try {
			argument.validate();
			fail("Expected ArgumentException");
		} catch (ArgumentException e) {
			// expect exception
		}

		// set valid value
		argument.setValue("xyz");
		try {
			argument.validate();
		} catch (ArgumentException e) {
			fail("ArgumentException: " + e.getMessage());
		}
	}

	/**
	 * Test equals.
	 */
	public void testEquals()
	{
		Argument argument = new Argument("test", 't', false);

		// valid - by string and character
		assertTrue("equals(test) == false", argument.equals("test"));
		assertTrue("equals(t) == false", argument.equals('t'));

		// valid - by argument
		Argument argumentX = new Argument("test", 't', false);
		assertTrue("equals(argumentX) == false", argument.equals(argumentX));

		// invalid - null
		assertFalse("equals(null) == true", argument.equals(null));

		// invalid - by string
		assertFalse("equals(null) == true", argument.equals("null"));
		assertFalse("equals(t) == true", argument.equals("t"));
		assertFalse("equals() == true", argument.equals(""));

		// invalid - by character
		assertFalse("equals('x') == true", argument.equals('x'));

		// invalid - by argument
		Argument argumentY = new Argument("test", 'y', false);
		assertFalse("equals(argumentY) == true", argument.equals(argumentY));
		Argument argumentZ = new Argument("yest", 't', false);
		assertFalse("equals(argumentZ) == true", argument.equals(argumentZ));
		Argument argumentA = new Argument("yest", 'z', false);
		assertFalse("equals(argumentA) == true", argument.equals(argumentA));
		Argument argumentB = new Argument(null, 'z', false);
		assertFalse("equals(argumentB) == true", argument.equals(argumentB));
		Argument argumentC = new Argument("yest", null, false);
		assertFalse("equals(argumentC) == true", argument.equals(argumentC));

		// invalid - by wrong type
		assertFalse("equals(100) == true", argument.equals(100));
	}
}
